export default {
  data () {
    return {
      active: 1,
      activeName: 'one',
      // 添加商品数据
      addGoodsform: {
        goods_name: '',
        goods_price: '',
        goods_weight: '',
        goods_number: '',
        goods_cat: [],
        promotion: true,
        pics: [],
        attrs: [],
        goods_introduce: ''
      },
      goodsCatOptions: [],
      editorOption: {
        placeholder: '请输入商品内容'
      },
      // 上传图片数据
      dialogImageUrl: '',
      dialogVisible: false,
      headersInfo: {
        Authorization: localStorage.getItem('token')
      }
    }
  },
  created () {
    // 获取商品分类
    this.getGoodsCat()
  },
  methods: {
    // Tab标签选中时触发
    tabChecked (current) {
      console.log(+current.index)
      this.active = +current.index + 1
    },
    // 点击下一步切换Tab标签
    nextStep (num, name) {
      this.active = num
      this.activeName = name
    },
    // 获取商品分类
    async getGoodsCat () {
      const res = await this.$axios.get('categories', {
        params: {
          type: 3
        }
      })
      // console.log(res)
      this.goodsCatOptions = res.data.data
    },
    // 上传图片
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 上传图片成功时的回调函数
    updateImgData (res) {
      // console.log(res.data.tmp_path)
      this.addGoodsform.pics.push({
        pic: res.data.tmp_path
      })
    },
    // 添加商品
    async addGoods () {
      const {
        /* eslint-disable */
        goods_name,
        goods_price,
        goods_weight,
        goods_number,
        goods_cat,
        pics,
        attrs,
        goods_introduce
      } = this.addGoodsform
      await this.$axios.post('goods', {
        goods_name,
        goods_price,
        goods_weight,
        goods_number,
        goods_cat: goods_cat.join(','),
        pics,
        attrs,
        goods_introduce
      })
      this.$message({
        type: 'success',
        message: '添加商品成功'
      })
      this.$router.push('/goods')
    }
  }
}
